package cate.game.role.task.conditiontask;

import cate.common.table.conditiontask.ConditionRow;
import cate.common.table.d.GDTask;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

public class ConditionPO extends RoleDependent {
    public ConditionPO() {}

    @NoteField(value = "任务完成操作对应tid")
    public int tId;

    @NoteField(value = "条件")
    public int conditionId;

    @NoteField(value = "检测值")
    public long value;

    @NoteField(value = "达成条件")
    public boolean done = false;

    public ConditionPO(Role role, int tId, int conditionId){
        this.tId = tId;
        this.conditionId = conditionId;
        this.initialize(role);
        ConditionInitValue.initOnCreate(this);
    }

    @JsonIgnore
    public ConditionRow getRow() {
        if (role == null) {
            logger.error("达成条件--未初始化");
            return null;
        }
        if (role.getGame().table.conditionTask.task.get(conditionId) == null) {
            return null;
        }
        return role.getGame().table.conditionTask.task.get(conditionId);
    }

    private void checkDone() {
        ConditionRow row = getRow();
        if (row == null) {
            return;
        }
        switch (row.checkOper) {
            case GDTask.CheckOper.EQ:
                this.done = value == row.value;
                break;
            case GDTask.CheckOper.LTE:
                this.done = value <= row.value;
                break;
            case GDTask.CheckOper.GTE:
                this.done = value >= row.value;
                break;
            default:
        }
    }

    public void setValue(long v, boolean add) {
        ConditionRow row = getRow();
        if (row == null) {
            return;
        }
        this.value = add ? this.value + v : v;
        checkDone();
    }

    public void onDaySpan() {
        ConditionRow row = getRow();
        if (row == null){
            return;
        }
        if (row.taskDuration == GDTask.Condition.DAY) {
            reset();
        }
    }

    public void onWeekSpan() {
        ConditionRow row = getRow();
        if (row == null){
            return;
        }
        if (row.taskDuration == GDTask.Condition.WEEK) {
            reset();
        }
    }

    public void onMonthSpan() {
        ConditionRow row = getRow();
        if (row == null){
            return;
        }
        if (row.taskDuration == GDTask.Condition.MONTH) {
            reset();
        }
    }

    /**
     * 重置值（因为跨天或者跨周或者跨月等原因）
     */
    public void reset() {
        this.value = 0;
        this.done = false;
    }


    /**
     * 不重置值
     */
    public void resetDone() {
        this.done = false;
    }


    public void reset(int taskDuration) {
        if (taskDuration == GDTask.Condition.PERIOD) {
            reset();
        }
    }
}
